import { classifyAdd, query, queryList, editState, classifyDel } from '../../api/module/classify'
import { Message } from 'element-ui';
const utils = require('../../assets/js/utils')
// import utils from '' //工具类 时间格式化等
const Classify = {
    namespaced: true,
    state: {
        addDialog: false,//新增分类弹窗
        addDetailDialog: false,//新增详细内容弹窗
        loading: true,//加载动画
        classifyRadio: 1,//新增分类哪一级
        addClassifyFormCopy: {},//初始值复制
        type_id: null, //1、jquery 2、javascript 3、vue
        addClassifyForm: {
            classifyOneVal: '',//输入一级分类的值
            classifyTwoVal: '',//输入二级分类的值
            classifyThreeVal: '',//输入三级分类的值
            classifyOneSelect: '',//选择一级分类的值
            classifyTwoSelect: '',//选择二级分类的值
            classifyEditId: null,//修改的id
        },
        classifyOneOption: [],//一级分类下拉
        classifyTwoOption: [],//二级分类下拉
        classifyOneTable: [],//一级分类表格
        classifyTwoTable: [],//二级分类表格
        classifyThreeTable: [],//二级分类表格
        classifyListId: null,//查询分类列表哪级菜单
        classifyDetailID: '',//添加三级分类详细的id
        /**搜索内容 */
        searchData: {
            name: '',
            classifyOneId: '',//二级菜单的一级id
            classifyTwoId: ''//三级菜单的二级id
        }
    },
    actions: {
        /**保存分类 */
        save ({ dispatch, state }, state1) {
            let data = {
                rank: state.classifyRadio,
                type_id: state.type_id,
                edit_id: state.addClassifyForm.classifyEditId,
            }
            switch (state.classifyRadio) {
                case 1:
                    data.name = state.addClassifyForm.classifyOneVal;//一级分类名称
                    break;
                case 2:
                    data.classify_one_id = state.addClassifyForm.classifyOneSelect;//选择一级分类的id
                    data.name = state.addClassifyForm.classifyTwoVal;//二级分类名称
                    break;
                case 3:
                    data.classify_one_id = state.addClassifyForm.classifyOneSelect;//选择一级分类的id
                    data.classify_two_id = state.addClassifyForm.classifyTwoSelect;//选择二级分类的id
                    data.name = state.addClassifyForm.classifyThreeVal;//二级分类名称
                    break;
            }
            classifyAdd(data).then(() => {
                /**state为true是保存 false是保存并继续 */
                if (state1) {
                    state.addDialog = false;
                }
                if (state.classifyRadio == 1) {
                    dispatch('seachQuery')
                }
                state.addClassifyForm.classifyOneVal = '';//输入一级分类的值
                state.addClassifyForm.classifyTwoVal = '';//输入二级分类的值
                state.addClassifyForm.classifyThreeVal = '';//输入三级分类的值
                dispatch('list')
                Message({
                    message: "保存成功",
                    type: 'success',
                })
            })
        },
        /**删除 */
        classifyDel ({ dispatch, state }, params) {
            console.log(state);
            classifyDel(params).then(() => {
                Message({
                    message: "删除成功",
                    type: 'success',
                })
                dispatch('list')
            })
        },
        /**修改分类状态 */
        classifyState ({ state }, params) {
            console.log(state);
            editState(params).then((result) => {
                console.log(result)
            })
        },
        /**查询分类下拉 */
        seachQuery ({ state }, _id) {
            let params = {
                rank: state.classifyRadio,
                type_id: state.type_id,
                classify_one_id: state.addClassifyForm.classifyOneSelect
            }
            query(params).then((result) => {
                if (params.rank == 3) {
                    _id != '参数' ? state.addClassifyForm.classifyTwoSelect = _id : state.addClassifyForm.classifyTwoSelect = '';
                    state.classifyTwoOption = result;
                    return
                }
                state.classifyOneOption = result;
            })
        },
        /**分类列表 this.JQueryState.classifyListId = 1;*/
        list ({ state }) {
            state.loading = true;
            let params = { type_id: state.type_id, rank: state.classifyListId, searchData: state.searchData }
            queryList(params).then((result) => {
                switch (state.classifyListId) {
                    case 1:
                        state.classifyOneTable = result.list.map(item => ({
                            _id: item._id,//一级分类ID
                            name: item.name,//一级分类名字
                            state: item.state == 1 ? true : false,//一级分类状态
                            updated: utils.formattingTime(item.updated),//一级分类更新时间
                            classify_two_num: item.classify_two.length,//二级分类嘞
                            classify_three_num: item.classify_three.length,//三级分类
                        }));
                        break;
                    case 2:
                        state.classifyTwoTable = result.list.map(item => ({
                            _id: item._id,//二级分类ID
                            name2: item.name,//二级分类名称
                            state: item.state == 1 ? true : false,//二级分类状态
                            updated: utils.formattingTime(item.updated),//二级分类更新时间
                            name: item.classify_one[0].name,//一级分类名称
                            classify_one_id: item.classify_one[0]._id,//一级分类ID
                            classify_three_num: item.classify_three.length//三级分类数量
                        }));
                        break;
                    case 3:
                        state.classifyThreeTable = result.list.map(item => ({
                            _id: item._id,//三级分类ID
                            name3: item.name,//三级分类名称
                            state: item.state == 1 ? true : false,//三级分类状态
                            updated: utils.formattingTime(item.updated),//三级分类更新时间
                            name: item.classify_one[0].name,
                            classify_one_id: item.classify_one[0]._id,
                            name2: item.classify_two[0].name,
                            classify_two_id: item.classify_two[0]._id,
                            summarize: item.summarize,//描述
                            argumentsArray: item.argumentsArray,//参数
                            demo: item.demo//示例
                        }));
                        break;
                }
                state.loading = false;
            })
        },


    },
    mutations: {},
    getters: {},
}
export default Classify